﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MTO.Library.Domain.Repositories;
using MTO.Library.Domain;
using MTO.Framework.Web.Mvc;
using System.Web.UI;
using Telerik.Web.Mvc;
using System.ComponentModel;

namespace MTO.Library.Web.Controllers.Customers
{
    public class CustomersController : Controller
    {
        private readonly ICustomerRepository customerRepository;
        private readonly IProductRepository productRepository;

        public CustomersController(ICustomerRepository customerRepository, IProductRepository productRepository)
        {
            this.customerRepository = customerRepository;
            this.productRepository = productRepository;
        }

        public ActionResult Index()
        {
            var gridCommand = new GridCommand();
            gridCommand.Page = 1;
            gridCommand.PageSize = 10;

            var model = new ViewModels.CustomerIndexViewModel()
            {
                CustomerGridModel = this.GetCustomerIndexGridModel(gridCommand, string.Empty)
            };

            return View(model);
        }

        public PartialViewResult Create()
        {
            var customer = new Customer();
            return PartialView("_CustomerForm", customer);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Customer customer, FormCollection collection)
        {
            if (this.ModelState.IsValid)
            {
                this.customerRepository.SaveOrUpdate(customer);

                try
                {
                    return Json(new JsonResponse());
                }
                catch (Exception ex)
                {
                }
            }

            return PartialView("_CustomerForm", customer);
        }

        public PartialViewResult Edit(int id)
        {
            var customer = this.customerRepository.GetById(id);
            return PartialView("_CustomerForm", customer);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, Customer customerFormViewModel, FormCollection collection)
        {
            if (this.ModelState.IsValid)
            {
                var customer = this.customerRepository.GetById(id);
                this.UpdateModel(customer);
                
                this.customerRepository.SaveOrUpdate(customer);

                return Json(new JsonResponse());
            }

            return PartialView("_CustomerForm", customerFormViewModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Delete(int id)
        {
            //var book = this.customerRepository.Single(x => x.CustomerId == id);

            //this.customerRepository.Delete(book);
            //UnitOfWork.Commit();

            return Json("ok");
        }

        [GridAction(EnableCustomBinding = true)]
        public ActionResult GetCustomerIndexGridData(GridCommand command, string searchKeyword)
        {
            return View(this.GetCustomerIndexGridModel(command, searchKeyword));
        }

        private GridModel GetCustomerIndexGridModel(GridCommand command, string searchKeyword)
        {
            // get the data needed for the grid
            var customers = this.customerRepository.Search(searchKeyword);

            // Mettre un sort par défaut si il n'y en a pas
            if (!command.SortDescriptors.Any())
            {
                command.SortDescriptors.Add(new SortDescriptor() { Member = "FirstName", SortDirection = ListSortDirection.Ascending });
            }

            var gridHelper = new Helpers.CustomerIndexGridModelHelper(command, customers);

            //Convert Entity to Model
            var data = Mappers.CustomerIndexGridModelMapper.Map(gridHelper.ProcessedDataSource);

            var gridModel = new GridModel();

            if (command.GroupDescriptors.Any())
            {
                var groups = gridHelper.ApplyGrouping(data.AsQueryable(), command.GroupDescriptors);

                gridModel.Data = groups;
            }
            else
            {
                gridModel.Data = data;
            }

            gridModel.Total = gridHelper.Total;
            return gridModel;
        }
    }
}
